using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Ent.Voice.Nomination;
using Ent.Voice.Organisation;
using Ent.Web.Helper;
public partial class ShowVote : System.Web.UI.Page
{
    private int id;
    private string name;
    private int opt;
    private int pnt;
    public ShowVote()
    {
        this.PreRender += new EventHandler(Vote_PreRender);
    }

    void Vote_PreRender(object sender, EventArgs e)
    {
        SetControl();
    }
    private void SetControl()
    {
        string name = Request.QueryString["name"];
        lblPara1.Text = "&nbsp;&nbsp;&nbsp;" + ResourceHelper.Get("MSG_VoteAlertPara1").Replace("%%name", "<b>" + name + "</b>");//Request.QueryString.ToString();
        lblPara1.CssClass = "text11";
        lblPara2.Text = "<br />&nbsp;&nbsp;&nbsp;" + ResourceHelper.Get("MSG_VoteAlertPara2");
        lblComment.Text = "&nbsp;&nbsp;&nbsp;" + ResourceHelper.Get("LBL_Comment");
        lblName.Text = "&nbsp;&nbsp;&nbsp;" + ResourceHelper.Get("LBL_Name");
        lblEmail.Text = "&nbsp;&nbsp;&nbsp;" + ResourceHelper.Get("LBL_Email");
        lblAge.Text = ResourceHelper.Get("LBL_Age");
        lblCountry.Text = ResourceHelper.Get("LBL_Country");
        txtName.CssClass = "textbox";
        txtEmail.CssClass = "textbox";
        txtComment.CssClass = "textbox";
        rfvName.ErrorMessage = ResourceHelper.Get("MSG_NameMandatory");
        rfvEmail.ErrorMessage = ResourceHelper.Get("MSG_EmailMandatory");
        cFeed.Vote.Option = Vote.GetOption(opt);
        cFeed.Vote.Points = pnt;
        cFeed.ShowButton = false;
        //btnSubmit.Attributes.Add("onclick", "parent.votewindow.hide()");
        SetJavascript();
    }
    private void SetJavascript()
    {
        VoteCriteria vc = Entry.GetVoteCriteria(id);
        if (vc.MandateField.ToLower() == "email" || vc.MandateField.ToLower() == "all")
        {
            rfvEmail.Enabled = true;
            revEmail.Enabled = true;
            txtEmail.CssClass = "textbox textboxMan";
        }
        else
        {
            rfvEmail.Enabled = false;
            revEmail.Enabled = true;
        }
        if (vc.MandateField.ToLower() == "name" || vc.MandateField.ToLower() == "all")
        {
            rfvName.Enabled = true;
            txtName.CssClass = "textbox textboxMan";
        }
        else
            rfvName.Enabled = false;
        string jScript = "";
        jScript += "<script type=\"text/javascript\">";
        jScript += "function CheckEligibility()";
        jScript += "{";
        jScript += "var eligible= true;";
        jScript += "try{";
        jScript += SetAgeCheck(vc.Age);
        jScript += SetCountryCheck(vc.Country);
        if (!txtAge.Visible && !ddlCountry.Visible)
        {
            lblProceed.Text = ResourceHelper.Get("MSG_ProceedToVote");
            lblProceed.Visible = true;
        }
        jScript += "}catch(e){}";
        jScript += "if(eligible)";
        jScript += "{";
        jScript += "document.getElementById('" + tblElg.ClientID + "').style.position='absolute';";
        jScript += "document.getElementById('" + tblElg.ClientID + "').style.visibility='hidden';";
        jScript += "document.getElementById('" + tblMain.ClientID + "').style.position='relative';";
        jScript += "document.getElementById('" + tblMain.ClientID + "').style.visibility='visible';";
        jScript += "document.getElementById('" + tblMain.ClientID + "').style.top='0px';";
        jScript += "document.getElementById('" + tblMain.ClientID + "').style.left='0px';";
        jScript += "}";
        jScript += "else";
        jScript += "{";
        jScript += "alert('" + ResourceHelper.Get("MSG_EligibleVote") + "');";
        jScript += "parent.votewindow.hide();";
        jScript += "}";
        jScript += "return false;";
        jScript += "}";
        jScript += "</script>";
        Page.RegisterClientScriptBlock("script_name", jScript);
    }
    private string SetAgeCheck(string val)
    {
        CriteriaHelper ch = Common.GetCriteria("Age");
        if (val == null || (ch.Criteria.ContainsKey(val) && ch.Criteria[val] == "any"))
        {
            txtAge.Visible = false;
            lblAge.Visible = false;
            return "";
        }
        string jScript = "";
        jScript += "var ageAct=document.getElementById(prefix + 'txtAge').value;";
        jScript += "if(ageAct=='') {alert('You must enter your age.'); return false;}";
        jScript += "var isInt=true; " + Ent.Web.Javascript.CheckInteger("ageAct", "isInt") + " if(!isInt) {alert('Age should be a valid integer.'); return false;}";
        jScript += "if(ageAct<5 || ageAct >100) {alert('Out of valid age range.'); return false;}";
        jScript += ch.GetJavascriptCase(val, "ageAct", "eligible");
        return jScript;
    }
    private string SetCountryCheck(string val)
    {
        CriteriaHelper ch = Common.GetCriteria("Country");
        if (val == null || (ch.Criteria.ContainsKey(val) && ch.Criteria[val] == "any"))
        {
            ddlCountry.Visible = false;
            lblCountry.Visible = false;
            return "";
        }
        foreach (string s in ch.Options)
        {
            if (ch.Criteria[s].ToLower() != "any")
                ddlCountry.Items.Add(s);
        }
        string jScript = "";
        jScript += "var countryCTRL=document.getElementById(prefix + 'ddlCountry');";
        jScript += "var countryAct=countryCTRL.options[countryCTRL.selectedIndex].value;";
        jScript += ch.GetJavascriptCase(val, "countryAct", "eligible");
        return jScript;
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            btnSubmit.Attributes.Add("onmouseover", "this.src='Images/dj/goh.gif'");
            btnSubmit.Attributes.Add("onmouseout", "this.src='Images/dj/go.gif'");
        }
        id = Convert.ToInt32(Request.QueryString["id"]);
        name = Request.QueryString["name"];
        opt = Convert.ToInt32(Request.QueryString["sel"]);
        pnt = Convert.ToInt32(Request.QueryString["pnt"]);
    }
    protected void btnSubmit_Click(object sender, ImageClickEventArgs e)
    {
        System.Net.IPHostEntry host = new System.Net.IPHostEntry();
        host = System.Net.Dns.GetHostByName(Request.ServerVariables["REMOTE_HOST"]);
        Vote v = new Vote();
        v.Email = txtEmail.Text;
        v.EntryId = id;
        v.IP = Request.ServerVariables.Get("REMOTE_ADDR");
        v.HostName = host.HostName;
        v.Name = txtName.Text;
        v.Comment = txtComment.Text;
        v.Option = Vote.GetOption(opt);
        v.Points = pnt;
        VoteResponse vr = v.AddVote(true);
        
        if (vr == VoteResponse.EntryAttempted || vr == VoteResponse.EventVoted)
            ShowWarn(vr);
        else if (vr == VoteResponse.Success || vr == VoteResponse.VerificationPending)
            ShowSuccess(vr, v);
    }
    protected void btnSubmitNoWarn_Click(object sender, ImageClickEventArgs e)
    {
        System.Net.IPHostEntry host = new System.Net.IPHostEntry();
        host = System.Net.Dns.GetHostByName(Request.ServerVariables["REMOTE_HOST"]);
        Vote v = new Vote();
        v.Email = txtEmail.Text;
        v.EntryId = id;
        v.IP = Request.ServerVariables.Get("REMOTE_ADDR");
        v.HostName = host.HostName;
        v.Name = txtName.Text;
        v.Comment = HttpUtility.HtmlEncode(txtComment.Text);
        v.Option = Vote.GetOption(opt);
        v.Points = pnt;
        ShowSuccess(v.AddVote(false), v);
    }
    private void ShowSuccess(VoteResponse vr, Vote v)
    {
        if (vr == VoteResponse.VerificationPending)
        {
            Ent.Web.Mail.Processor p = new Ent.Web.Mail.Processor();
            Ent.Web.Mail.MailContent cont = Ent.Web.Mail.Collection.GetMailContent("VoteSubmitted");
            p.To = txtEmail.Text;
            p.Subject = cont.Subject;
            p.Body = string.Format(cont.Body, v.Name,
                ConfigurationManager.AppSettings.Get("BaseUrl") + "/ShowEntry.aspx?id=" + v.EntryId.ToString(),
                ConfigurationManager.AppSettings.Get("BaseUrl") + "/ActivateFeed.aspx?id=" + v.VoteId.ToString() + "&pass=" + v.IP.ToString()
                );
            p.SendMail();
        }
        SetMainAsFirstPage();
        trCtrl.Visible = false;
        lblMessage.Text = GetMessage(vr) + "<br/>";
        btnSubmitNoWarn.Visible = false;
        btnCancelNoWarn.ImageUrl = "Images/dj/close.gif";
        btnCancelNoWarn.Attributes.Add("onmouseover", "this.src='Images/dj/closeh.gif'");
        btnCancelNoWarn.Attributes.Add("onmouseout", "this.src='Images/dj/close.gif'");
    }
    private void ShowWarn(VoteResponse vr)
    {
        SetMainAsFirstPage();
        lblMessage.Text = GetMessage(vr) + "<br/>";
        btnSubmit.Visible = false;
        btnSubmitNoWarn.Attributes.Add("onmouseover", "this.src='Images/dj/goh.gif'");
        btnSubmitNoWarn.Attributes.Add("onmouseout", "this.src='Images/dj/go.gif'");
        btnCancelNoWarn.Attributes.Add("onmouseover", "this.src='Images/dj/cancelh.gif'");
        btnCancelNoWarn.Attributes.Add("onmouseout", "this.src='Images/dj/cancel.gif'");
    }
    private void SetMainAsFirstPage()
    {
        tblMain.Style.Add(HtmlTextWriterStyle.Position, "relative");
        tblMain.Style.Add(HtmlTextWriterStyle.Visibility, "visible");
        tblMain.Style.Add(HtmlTextWriterStyle.Top, "0px");
        tblMain.Style.Add(HtmlTextWriterStyle.Left, "0px");
        tblElg.Visible = false;
        trMsg.Visible = true;
        btnCancelNoWarn.Attributes.Add("onclick", "document.body.innerHTML=''; parent.votewindow.hide();");
    }
    private string GetMessage(VoteResponse vr)
    {
        switch (vr)
        {
            case VoteResponse.Success:
                return Common.GetSuccessText(ResourceHelper.Get("MSG_VoteSuccess"));
            case VoteResponse.VerificationPending:
                return Common.GetAlertText(ResourceHelper.Get("MSG_VoteVerification"));
            case VoteResponse.EventVoted:
                return Common.GetAlertText(ResourceHelper.Get("MSG_VoteVotedWarn"));
            case VoteResponse.EntryAttempted:
                return Common.GetAlertText(ResourceHelper.Get("MSG_VoteAttemptedWarn"));
            default:
                return "";
        }
    }
}
